package cn.learn.controller.cargo;

import cn.learn.base.BaseController;
import cn.learn.domain.cargo.PackingList;
import cn.learn.domain.cargo.PackingListExample;
import cn.learn.domain.cargo.ShippingOrder;
import cn.learn.domain.cargo.ShippingOrderExample;
import cn.learn.service.cargo.PackingListService;
import cn.learn.service.cargo.ShippingOrderService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping(value = "/cargo/shipping")
public class ShippingOrderController extends BaseController{

    @Reference
    private ShippingOrderService shippingOrderService;
    @Reference
    private PackingListService packingListService;

    @RequestMapping(value = "/toShipping",name = "到生成委托单页面")
    public String toShipping(String id) {
        request.setAttribute("id", id);
        return "/cargo/ship/shipping-add";
    }

    @RequestMapping(value = "/edit", name = "添加委托单")
    public String edit(ShippingOrder shippingOrder) {
        shippingOrder.setCompanyId(companyId);
        shippingOrder.setCompanyName(companyName);
        shippingOrderService.save(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }
    @RequestMapping(value = "list",name = "查询委托单")
    public String list(@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4")int pageSize) {
        ShippingOrderExample shippingOrderExample = new ShippingOrderExample();
        shippingOrderExample.setOrderByClause("create_time desc");
        ShippingOrderExample.Criteria criteria = shippingOrderExample.createCriteria();
        criteria.andCompanyIdEqualTo(companyId);
        PageInfo pb = shippingOrderService.findAll(shippingOrderExample, pageNum, pageSize);
        request.setAttribute("pageList", pb);
        return "/cargo/ship/shipping-list";
    }

    @RequestMapping(value = "/toView", name = "查看委托单")
    public String toView(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        request.setAttribute("shippingOrder",shippingOrder);
        return "/cargo/ship/shipping-view";
    }

    @RequestMapping(value = "/toUpdate", name = "到修改委托单页面")
    public String toUpdate(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        request.setAttribute("shipping",shippingOrder);
        return "/cargo/ship/shipping-update";
    }

    @RequestMapping(value = "/update", name = "修改委托单")
    public String update(ShippingOrder shippingOrder) {
        shippingOrderService.update(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/submit", name = "提交委托单状态为1")
    public String submit(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        shippingOrder.setState(1);
        shippingOrderService.update(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/cancel", name = "取消委托单状态为0")
    public String cancel(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        shippingOrder.setState(0);
        shippingOrderService.update(shippingOrder);
        return "redirect:/cargo/shipping/list.do";
    }

    @RequestMapping(value = "/delete", name = "删除委托单")
    public String delete(String id) {
        shippingOrderService.delete(id);
        return "redirect:/cargo/shipping/list.do";
    }
    @RequestMapping(value = "toInvoice", name = "获取委托单状态并返回")
    @ResponseBody
    public String toInvoice(String id) {
        ShippingOrder shippingOrder = shippingOrderService.findById(id);
        return shippingOrder.getState().toString();
    }
}
